//
//  MCLoginPasswordViewController.swift
//  StoreManage
//
//  Created by MC on 2018/8/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCLoginPasswordViewControllerClosure = (UIViewController) -> Void

class MCLoginPasswordViewController: MCViewController {

    
    var closure : MCLoginPasswordViewControllerClosure?
    
    
    public var fatherVC : UIViewController?
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.endEditing(true)
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func forgetButtonClicked() {
        
        let vc = MCChangePasswordOneViewController()
        vc.fatherVC = fatherVC
        vc.phoneNumber = phoneTextField.text ?? ""
        closure?(vc)
        
    }
    
    
    @objc func loginButtonClicked() {
        
        let phone = phoneTextField.text ?? ""

        let password = passwordTextField.text ?? ""
        
        
        guard (phone.count == 11 && phone.count != 0) else {
            MCAlterController_oneOptions(vc: self, message: "账号或者密码不正确", cancle: { })
            return
        }
        
        
        
        let para = [
            "userName" : phone,
            "password" : password.MCMD5()
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_login, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in
            
            let dataDcit = response["data"] as? [String:Any] ?? [:]
            
            
            
            self.view.endEditing(true)
            
            MCHUD_text(message: "登录成功", view: self.view, delay: 1)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                let vc = MCChangeStoreViewController()
                vc.isLoginVCJump = true
                vc.userDict = dataDcit
                let nav = MCNavigationController.init(rootViewController: vc)
                self.fatherVC!.present(nav, animated: true, completion: nil)
                
            })
            
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    
    // MARK: - Method
    func baseSetting() { }
    
    
    func initUI() {
        
        view.addSubview(phoneTextField)
        phoneTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(25)
            make.height.equalTo(40)
        }
        
        view.addSubview(passwordTextField)
        passwordTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(phoneTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        view.addSubview(forgetButton)
        forgetButton.snp.remakeConstraints { (make) ->Void in
            make.right.top.bottom.equalTo(passwordTextField)
            make.width.equalTo(100)
        }
        

        view.addSubview(loginButton)
        loginButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(passwordTextField.snp.bottom).offset(40)
            make.height.equalTo(40)
        }
        
        
        view.addSubview(promptImageView)
        promptImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(MCAdaptationModel(-30))
        }
        
        view.addSubview(logoImageView)
        logoImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize.init(width: 50, height: 50))
            make.bottom.equalTo(promptImageView.snp.top).offset(MCAdaptationModel(-20))
        }
    }
    
    // MARK: - Setter & Getter
    lazy var logoImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Logo")
        return imageView
    }()
    
    lazy var promptImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Login_prompt")
        return imageView
    }()
    
    lazy var phoneTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入手机号码"
        textField.font = MCFont(14)
        textField.keyboardType = UIKeyboardType.numberPad
        return textField
    }()
    
    lazy var passwordTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入密码"
        textField.keyBoardType = MCKeyBorardType.asciiCapable
        textField.isSecureTextEntry = true
        textField.font = MCFont(14)
        return textField
    }()
    
    
    lazy var forgetButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitle("| 忘记密码", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.addTarget(self, action: #selector(forgetButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var loginButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 18
        button.layer.masksToBounds = true
        button.setTitle("登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(loginButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

}
